package com.mindcraft.shop.api;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mindcraft.base.model.MindCraftResult;
import com.mindcraft.base.utils.CommonConst;
import com.mindcraft.base.utils.JwtHelper;
import com.mindcraft.shop.po.*;
import com.mindcraft.shop.service.CategoryService;
import com.mindcraft.shop.service.CommentService;
import com.mindcraft.shop.service.CommunityService;
import com.mindcraft.shop.utils.LoginCheck;
import com.mindcraft.shop.vo.*;
import com.mindcraft.user.utils.MindCraftUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;

@Api(value = "shop", tags = "商城广场接口")
@RestController
@RequestMapping("/shop/square")
public class SquareController {


    @Autowired
    private CommunityService communityService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private CommentService commentService;

    @ApiOperation("广场列表")
    @PostMapping("getRecordList")
    public MindCraftResult<Page> getRecordList(@RequestBody RecordInfoQueryVo recordInfoVo) {

        return communityService.listRecord(recordInfoVo);

    }

    @ApiOperation("广场导航")
    @GetMapping("getNavigation")
    public MindCraftResult getNavigationList() {
        return MindCraftResult.success(categoryService.list(
                new LambdaQueryWrapper<Category>().eq(Category::getBelong, CommonConst.CATEGORY_Square)

        ));
    }


    /**
     * 查询评论
     */
    @ApiOperation("查询评论")
    @PostMapping("/listComment")
    public MindCraftResult<CommentRequestVo> listComment(@RequestBody CommentRequestVo commentRequestVo) {
        return commentService.listComment(commentRequestVo);
    }

    /**
     * 点赞评论
     */
    @ApiOperation("点赞评论")
    @PostMapping("/like")
    @LoginCheck
    public MindCraftResult like(@RequestBody Like like) {
        String token = MindCraftUtil.getRequest().getHeader(CommonConst.TOKEN);
        String userId = JwtHelper.getUserId(token);
        like.setUserId(userId);
        return  communityService.like(like);
    }

    @ApiOperation("发表评论")
    @PostMapping("/sendComment")
    @LoginCheck
    public MindCraftResult sendComment(@RequestBody CommentVo commentVo) {
        String token = MindCraftUtil.getRequest().getHeader(CommonConst.TOKEN);
        String userId = JwtHelper.getUserId(token);
        String userName = JwtHelper.getUserName(token);
        commentVo.setUsername(userName);
        commentVo.setUserId(userId);
        return  communityService.sendComment(commentVo);
    }

//    @PostMapping("/findChidrenComment")
//    @ApiOperation("根据父评论查询子评论")
//    public MindCraftResult findChidrenComment(@RequestBody CommentVo commentVo) {
//
//
//        return MindCraftResult.success(commentService.findChidrenComment(commentVo));
//    }



}
